'use strict';


var path = require('path');
var Etcd = require('node-etcd');

// config
var etcd_host = '192.168.102.142';
// var etcd_host = 'localhost';
var etcd_prot = '4001';
var server_port = 3000;


// -------- get ip
var ifaces = require('os').networkInterfaces();
var ips = {};
for (var dev in ifaces) {
	ifaces[dev].forEach(function(details) {
		if (details.family === 'IPv4') {
			ips[dev] = details.address;
		}
	});
}
// console.log(ips);
var ip_eth0 = ips.eth0;
console.log('ip_eth0=%s', ip_eth0);


// -------- register to etcd
var etcd = new Etcd(etcd_host, etcd_prot);
var server_name = require('./package.json').name;
var p = path.join('/services', server_name);
var server_info = {
	host: ip_eth0,
	port: server_port,
	usage: 'http://host:port/' // additional info
};
var server_info_str = JSON.stringify(server_info);
// etcd.set(p, server_info_str);
etcd.set(p, server_info_str, {ttl:10}); // time to live
console.log('register to etcd path= %s, info = %s', p, server_info_str);



// -------- start the server add
var http = require('http');
var server = http.createServer(function(req, res) {
	console.log('--request--');
	res.end('service_a');
});
server.listen(server_port, '127.0.0.1');
console.log('server start at %s', server_port);